﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bauglir.Ex;
using System.Net;
using System.Windows.Controls;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using Bauglir.Ex;

namespace music_control
{
    public class MusicPluginChrome : IMusicPlugin, INotifyPropertyChanged
    {
        private WebSocketServer WebSocketServer = new WebSocketServer();

        private string _PluginName = "Chrome Plugin Server";
        private MusicPluginChromeOptions _Options;

        public MusicPluginChrome()
        {
            _Options = new MusicPluginChromeOptions(this);
        }



        private string _IPAdress = "127.0.0.1";
        public string IPAdress
        {
            get
            {
                return this._IPAdress;
            }

            set
            {
                this._IPAdress = value;
            }
        }


        private int _Port;
        public int Port
        {
            get
            {
                if (_Port == 0)
                    this._Port = Properties.Settings.Default.chromeport;

                return this._Port;
            }

            set
            {
                Properties.Settings.Default.chromeport = value;
                this._Port = value;
                ViewModel.isChanged = true;
                RaisePropertyChanged("Port");
            }
        }


        public string ServerState
        {
            get
            {
                if (GetStatus() == StatusEnum.StartedConnected)
                {
                    return "Started";
                }
                else if (GetStatus() == StatusEnum.StartedNotConnected)
                {
                    return "Started";
                }
                else if (GetStatus() == StatusEnum.Stopped)
                {
                    return "Stopped.";
                }

                return "no information available";

            }
        }

        public string ConnectionState
        {
            get
            {
                if (GetStatus() == StatusEnum.StartedConnected)
                {
                    return "Connected";
                }
                else
                {
                    return "Not connected";
                }
            }
        }


        public void StartPlugin()
        {
            if (!WebSocketServer.IsRunning)
            {
                WebSocketServer.Start(IPAddress.Parse(_IPAdress), Port);



                WebSocketServer.AfterAddConnection += new WebSocketServer.ConnectionEvent(WebSocketServer_ConnectionChanged);
                WebSocketServer.AfterRemoveConnection += new WebSocketServer.ConnectionEvent(WebSocketServer_ConnectionChanged);
            }
        }

        void WebSocketServer_ConnectionChanged(WebSocketServer aServer, WebSocketServerConnection aConnection)
        {
            RaisePropertyChanged("ServerState");
            RaisePropertyChanged("ConnectionState");
        }

        public void StopPlugin()
        {
            WebSocketServer.Stop();
        }

        public StatusEnum GetStatus()
        {
            if (WebSocketServer.IsRunning)
            {
                if (WebSocketServer.ConnectionCount >= 1)
                {
                    return StatusEnum.StartedConnected;
                }
                else
                {
                    return StatusEnum.StartedNotConnected;
                }
            }
            else
            {
                return StatusEnum.Stopped;
            }
        }

        public bool StartStopMusic()
        {
            if (GetStatus() == StatusEnum.StartedConnected)
            {
                for (int i = 0; i < WebSocketServer.ConnectionCount; i++)
                {
                    WebSocketServer.GetConnection(i).SendText("{\"action\": \"playpause\"}");
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool NextTrack()
        {
            if (GetStatus() == StatusEnum.StartedConnected)
            {
                for (int i = 0; i < WebSocketServer.ConnectionCount; i++)
                {
                    WebSocketServer.GetConnection(i).SendText("{\"action\": \"next\"}");
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool PreviouseTrack()
        {
            if (GetStatus() == StatusEnum.StartedConnected)
            {
                for (int i = 0; i < WebSocketServer.ConnectionCount; i++)
                {
                    WebSocketServer.GetConnection(i).SendText("{\"action\": \"previous\"}");
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public UserControl GetOptionsControl()
        {
            return _Options;
        }

        public string GetName()
        {
            return _PluginName;
        }

        public void ClosePlugin()
        {
            WebSocketServer.Stop();
        }


        public void ResetSettings()
        {
            Port = Properties.Settings.Default.chromeport = Properties.Settings.Default.chromeport_default;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }


        public void Save()
        {
            if (this.Port != WebSocketServer.Port)
            {
                StartPlugin();
            }
        }


    }
}
